package com.doom.a3rdlibdemo;

import android.os.Handler;
import android.os.Looper;

public class ObservableObserveOn<T> extends AbstractObservableWithUpstream<T, R>{

//    ObservableSource<T> souce;

    public ObservableObserveOn(ObservableSource<T> source) {
        super(source);
    }

    @Override
    protected void subscribeActual(Observer<T> observer) {
        final ObserverOnObserver parent = new ObserverOnObserver<T>(observer);
        source.subscribe(parent);
    }

    static final class ObserverOnObserver<T> implements Observer<T> {
        final Observer<T> actual;

        public ObserverOnObserver(Observer<T> actual) {
            this.actual = actual;
            handler = new Handler(Looper.getMainLooper());
        }

        private Handler handler;

        @Override
        public void onSubscribe() {

        }

        @Override
        public void onNext(final T t) {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    actual.onNext(t);
                }
            });
        }

        @Override
        public void onError(Throwable e) {

        }

        @Override
        public void onComplete() {

        }
    }

}
